package com.shuyuntu.proposal.service;

import com.shuyuntu.proposal.dao.IArticleDao;
import com.shuyuntu.proposal.entity.Article;
import com.shuyuntu.proposal.entity.Delegate;
import com.shuyuntu.proposal.enums.EArticleType;
import com.shuyuntu.proposal.enums.EProposalState;
import com.shuyuntu.proposal.enums.EProposalType;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by limeng on 16/8/22.
 */
@Service
@Transactional
public class ArticleService {
    /*===*** injection ***===*/
    @Resource
    private IArticleDao articleDao;

    public IArticleDao getArticleDao() {
        return articleDao;
    }

    public void setArticleDao(IArticleDao articleDao) {
        this.articleDao = articleDao;
    }

    public boolean publishArticle(Article article) {
        try {
            articleDao.insert(article);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    public Article getArticleById(Integer articlelId) {
        return articleDao.queryById(articlelId);
    }

    public boolean updateArticle(Article article) {
        try {
            articleDao.update(article);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean deleteArticle(Article article) {
        try {
            articleDao.delete(article);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;    }

    public List<Article> doSearch(Integer pageSize, Integer pageNo, Map<String, Object> reqMap, StringBuffer rowCount) {
        Criterion[] criterionArr = new Criterion[12];
        //数组游标,此处不能使用List,只能用这种方式
        int cursor = 0;

        Object authorId = reqMap.get("authorId");
        Object title = reqMap.get("title");
        Object type = reqMap.get("type");
        Object startTime = reqMap.get("startTime");
        Object endTime = reqMap.get("endTime");

        if (authorId != null) {
            criterionArr[cursor++] = (Restrictions.eq("author.id", authorId));
        }
        if (title != null && !title.equals("")) {
            criterionArr[cursor++] = (Restrictions.like(
                    "title", (String)title, MatchMode.ANYWHERE));
        }
        if (type != null) {
            criterionArr[cursor++] = (Restrictions.eq(
                    "type", EArticleType.values()[(Integer) type]));
        }
        SimpleDateFormat sdf = null;
        if (startTime != null) {
            sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date startDate = sdf.parse((String)startTime);
                criterionArr[cursor++] = Restrictions.ge("updateTime", startDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if (endTime != null) {
            if (sdf == null) {
                sdf = new SimpleDateFormat("yyyy-MM-dd");
            }
            try {
                Date endDate = sdf.parse((String)endTime);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(endDate);
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                endDate = calendar.getTime();
                criterionArr[cursor] = Restrictions.le("updateTime", endDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        return articleDao.querySearchPageList(pageNo, pageSize, rowCount, Order.desc("id"), criterionArr);
    }

    public Article getArticleAndIncreaseClickCountById(Integer articleId) {
        Article article = articleDao.queryById(articleId);
        article.setClickCount(article.getClickCount() + 1);
        articleDao.update(article);

        return article;
    }

    /*------------------------------------*/




}
